package com.kyzh.core.pager.weal.community

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.CommunityPingLunBean
import com.kingja.loadsir.core.LoadSir
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemFragCommunityNewsBinding
import com.kyzh.core.databinding.ViewSwipeRevBinding
import com.kyzh.core.dialog.showSendMessageDialog
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import com.kyzh.core.utils.setVisibility
import org.jetbrains.anko.support.v4.onRefresh

class NewsFragment : BaseFragment() {
    private val vm  by viewModels<CommunityViewModel>()
    private lateinit var db :ViewSwipeRevBinding
    private var mp = 1
    private val adapter  = Adapter()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db = ViewSwipeRevBinding.inflate(inflater)
        loadSir = LoadSir.getDefault().register(db.root) {
            reloadClick()
        }
        db.lifecycleOwner = this
        return loadSir.loadLayout
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.rev.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        vm.wodehuifu(mp) {
            loadSir.showSuccess()
            adapter.setNewInstance(this.data)
            if(mp>=maxP){
                adapter.loadMoreModule.loadMoreEnd(true)
            }
        }
        db.swipe.onRefresh {
            mp = 1
            vm.wodehuifu(mp) {
                db.swipe.isRefreshing = false
                loadSir.showSuccess()
                adapter.setNewInstance(this.data)
                if(mp>=maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mp ++
            vm.wodehuifu(mp) {
                db.swipe.isRefreshing = false
                loadSir.showSuccess()
                adapter.addData(this.data)
                adapter.loadMoreModule.loadMoreComplete()
                if(mp>=maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.setOnItemChildClickListener{a,v,p->
            val data = a.data[p] as CommunityPingLunBean.Data
            when(v.id){
                R.id.back ->{
                    requireActivity().showSendMessageDialog("回复@${data.petName}") {
                        vm.huifu("1",this,data.id,data.uid){

                        }
                    }
                }
            }
        }
    }
    private fun reloadClick() {
    }
    private inner class Adapter :BaseQuickAdapter<CommunityPingLunBean.Data,BaseDataBindingHolder<ItemFragCommunityNewsBinding>>(
        R.layout.item_frag_community_news),LoadMoreModule{
        init {
            addChildClickViewIds(R.id.back)
        }
        override fun convert(
            holder: BaseDataBindingHolder<ItemFragCommunityNewsBinding>,
            item: CommunityPingLunBean.Data
        ) {
           holder.dataBinding?.apply {
               d = item
               tvTag2.setVisibility(!item.video.isNullOrBlank())
               when {
                   item.isBest=="1" -> {
                       tvTag1.setVisibility(true)
                       tvTag1.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#FA8C08"))
                       tvTag1.text = "置顶"
                   }
                   item.isJiajing == "1" -> {
                       tvTag1.setVisibility(true)
                       tvTag1.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#FF726A"))
                       tvTag1.text = "精华"
                   }
                   else -> {
                       tvTag1.setVisibility(false)
                   }
               }
           }
        }

    }
}